<?php
/**
 *  The constructor implementation using config
 *
 */
class Yz_Descriptor_Default
    extends Yz_Hash
#    extends Yz_Descriptor_Abstract
    implements Yz_Descriptor_Interface
{

    /** inherited from Yz_Hash
    *   @var array value
    **/
    protected $value = array();


    /************************************
     *          Public methods          *
     ************************************/

    /**
     *  @param Yz_Config_Interface with the following entries:
     *      'property' (hash, required) : the configuration of properties
     */
    public static function create(Yz_Hash $config)
    {
        debug();
        $defaultSource = 't0'; #$model->describeSource()->key();
        $descr = new static();
        foreach ($config as $propName => $propConfig) {
            if (null === $propConfig->source) {
                $propConfig->source = $defaultSource;
            }
            $descr->createProperty($propName, $propConfig);
        }
        return $descr;
    }



    protected function createProperty($propName, $propConfig)
    {
        debug($propName);
        // TODO what if $propConfig is not a config, or has no type
        $type = $propConfig->type;
        if (null === $type) {
            throw new Yz_Exception("Missing type specification in config for property {$propName}");
        }
        debug("Prop<$propName>=$type");
        // TODO class name configurable
        $propClass = 'Yz_Property_' . ucfirst($type);
        $this->value[$propName] = new $propClass($propName,$propConfig);
        return $this;
    }

}
